package mysql

import (
	"gorm.io/gorm"
	"time"
)

type Order struct {
	gorm.Model
	UserId    int     `gorm:"type:tinyint;not null;comment:用户ID"`
	OrderSn   string  `gorm:"type:varchar(36);not null;comment:订单唯一编号"`
	PayType   int     `gorm:"int(1);default:1;comment:1-支付宝 2-微信"`
	PayStatus int     `gorm:"int(1);default:1;comment:1-待支付 2-已支付 3-已取消"`
	Amount    float64 `gorm:"type:decimal(10,2);not null;comment:总价"`
}

type OrderCourse struct {
	gorm.Model
	CourseId int    `gorm:"type:tinyint;not null;comment:课程ID"`
	OrderId  int    `gorm:"type:tinyint;not null;comment:订单ID"`
	Name     string `gorm:"type:varchar(20);not null;comment:课程名称"`
	Intro    string `gorm:"type:varchar(25);not null;comment:简介"`
}

func (o *Order) Add() error {
	return DB.Create(&o).Error
}
func (oc *OrderCourse) Add() error {
	return DB.Create(&oc).Error
}
func (o *Order) List(startTime, endTime int64, orderSn string) (order []Order, err error) {
	st := time.Unix(startTime, 0).Format("2006-01-02 15:04:05.000")
	et := time.Unix(endTime, 0).Format("2006-01-02 15:04:05.000")
	err = DB.Where("created_at<=? AND created_at<=?", st, et).Where("order_sn = ?", orderSn).Find(&order).Error
	return order, err
}
